package cn.aitstone.controller;

import cn.aitstone.entity.User;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RefreshScope
public class UserController {

    @Value("${test}")
    private Long test;

    @GetMapping("/getByUserId/{id}")
    @SentinelResource(value = "user", blockHandler = "exceptionHandler",
    fallback = "getByIdfallback")
    public User getByUserId(@PathVariable Long id){
        int a = 1 / 0;
        return User.builder().id(test).name("李白").intro("诗仙").build();
    }

    public User exceptionHandler(@PathVariable Long id, BlockException ex){
        return User.builder().id(-1L).name("限流啦").intro("稍后重试").build();
    }

    /**
    * @Title: getByIdfallback
    * @Description: 熔断降级
    * @Author: Sheldon
    * @Version: 1.0
    * @Date:  2022/8/1 20:59
    * @Parameters: [id, ex]
    * @Return cn.aitstone.entity.User
    */
    public User getByIdfallback(@PathVariable Long id){
        return User.builder().id(-2L).name("熔断啦").intro("熔断降级啦").build();
    }
}
